using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

/**
*@file       LSRole_CreateReciever.cs
*@brief      process message LSRole_Create
*
*@author     graywind
*@version    1.0.0.1
*@data       2012/4/14
*/

//process LSRole_Create
public class OnLSRole_Create
{
    /// <summary>
    /// process message
    /// </summary>
    /// <param name="op">message operator</param>
    /// <param name="session_id">session identifier</param>
    /// <param name="msg_buffer">message buffer</param>
    /// <param name="msg_bytes">buffer length</param>
    public static void OnMapping(IMessageOperator op, Int32 session_id, byte[] msg_buffer, UInt32 msg_bytes)
    {
        //TODO
		loginserver.UsrCreateRole ucr = ProtoNet<loginserver.UsrCreateRole>.CreateFromBytes(msg_buffer, msg_bytes);

        int error = -1;

        do
        {
            if (ucr.JobType < 0)
            {
                error = (int)dbserver.CreateRoleFailed.eErrorCode.JobType_Invalid;
                break;
            }

            if (!op.CheckPassport(ucr.UsrId, ucr.Passport))
            {
                error = (int)dbserver.CreateRoleFailed.eErrorCode.Passport_Invalid;
                break;
            }

            int login_code = op.LoginManager.GetLoginCode(session_id);
            if (login_code == UsrLoginManager.INVALID_CODE)
            {
                error = (int)dbserver.CreateRoleFailed.eErrorCode.LoginCode_Invalid;
                break;
            }
			
			int server_session = op.GetDBHandle();
			if(server_session == NetLib.INVALID_SESSION)
			{
				error = (int)dbserver.CreateRoleFailed.eErrorCode.ServerNotPrepared;
				break;	
			}
			msgDBRole_Create msg = new msgDBRole_Create(server_session, ucr.UsrId, ucr.RoleName, ucr.Gender, ucr.JobType, login_code);
            gwMessagePort.ProcessMessage(msg);

        } while (false);
        
        if(error >= 0)
        {
			msgLSRole_Create_Failed msg = new msgLSRole_Create_Failed(session_id, (dbserver.CreateRoleFailed.eErrorCode)error);
            gwMessagePort.ProcessMessage(msg);
        }
    }
}

//register callback
partial class MessageProcTypes<ProcType, MessageProc>
{
        [MessageProcType((int)LoginServerTypes.MessageType.LSRole_Create)]
        public static LoginServerProcessor.LoginServerTabFunc LSRole_CreateProc = new LoginServerProcessor.LoginServerTabFunc(OnLSRole_Create.OnMapping);
}
